// pages/shopCart/shopCart.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
Page({
  data: {
    page:1,
    items: [],
    startX: 0,
    startY: 0,
    // mengbanBol: true,
    //全选
    checkAll: false,
    //总金额
    pay_price: 0,
    checkNum:0,
    //是否登录
    loginInfo:true,
    wpxIos:false,
    open:false,
    list:[],
    //已触底
    chudi: false,
    jiazai: false,
  },
  //去购物车订单详情页
  goDetail() {
    wx.navigateTo({
      url: '/pages/ShoppingCart/cartDetail/cartDetail'
    })
  },

  //空函数
  kongHanShu() {

  },
  //选择订单
  checkboxChange(e) {
    if (e.detail.value.length >= this.data.items.length) {
      this.setData({
        checkAll: true
      })
    } else {
      this.setData({
        checkAll: false
      })
    }
    this.setData({
      checkNum:0
    })
    this.data.items.forEach((ele, index) => {
      let num = index.toString()
      if (e.detail.value.indexOf(num) == -1) {
        var str = "items[" + index + "].selected"
        this.setData({
          [str]: false
        })
      } else {
        var str = "items[" + index + "].selected"
        this.setData({
          [str]: true,
          checkNum: this.data.checkNum + 1
        })
      }
    })
    this.wpxjisuan()
  },
  //全选
  checkboxAll(e) {
    if (e.detail.value[0]) {
      this.setData({
        checkAll: true
      })
      this.data.items.forEach((ele, index) => {
        var str = "items[" + index + "].selected"
        this.setData({
          [str]: true
        })
      })
      this.setData({
        checkNum: this.data.items.length
      })
    } else {
      this.setData({
        checkAll: false
      })
      this.data.items.forEach((ele, index) => {
        var str = "items[" + index + "].selected"
        this.setData({
          [str]: false
        })
      })
      this.setData({
        checkNum: 0
      })
    }
    this.wpxjisuan()
  },
  setNum(e){
    let ind = e.target.dataset.index;
    let num=Number(e.detail.value)
    var str = "items[" + ind + "].num"
    if (num<1) {
      this.setData({
        [str]:1
      })
    }else{
      this.setData({
        [str]:num
      })
    }
    this.wpxjisuan()
  },
  //减
  reduce: function(e) {
    let ind = e.target.dataset.index;
    var num = this.data.items[ind].num;
    if (1 < num) {
      num--;
    } else {
      num = 1;
    }
    var str = "items[" + ind + "].num"
    this.setData({
      [str]: num
    })
    this.wpxjisuan()
  },
  //加
  add: function(e) {
    let ind = e.target.dataset.index;
    var num = this.data.items[ind].num;
    num++;
    var str = "items[" + ind + "].num"
    this.setData({
      [str]: num
    })
    this.wpxjisuan()
  },

  //计算总价
  wpxjisuan(){
    wx.showLoading({
      title: '计算金额中',
    })
    let obj={
      'distribute_type':this.data.items[0].delivery_service_id,
      'data':this.data.items
    }
    JsonPost('order/pay-price',obj).then(res=>{
      this.setData({
        goods_price:res.data.goods_price,
        pay_price:res.data.pay_price,
        service_price:res.data.service_price,
      })
      wx.hideLoading()
    })
  },

  // 打开规格
  openFormat(e){
    this.selectComponent("#formatBox").setData({
      goodInfo:e.currentTarget.dataset.row,
      cat_id:e.currentTarget.dataset.row.cat_id,
    })
    this.selectComponent("#formatBox").getInfo(e.currentTarget.dataset.row.goods_id)
    this.setData({
      open:true
    })    
  },


  onLoad: function(options) {
    this.setData({
      wpxIos: getApp().globalData.wpxIos
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 3
      })
    }
    this.setData({
      items:[],
      page:1
    })
    this.getList()
    this.setData({
      checkAll: false,
      pay_price: 0,
      checkNum: 0
    })
  },

  //去登陆
  goLogin(){
    wx.switchTab({
      url: '/pages/personCenter/personCenter',
    })
  },

  //去逛逛
  goHome(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  //获取购物车列表
  getList() {
    JsonPost('cat/list',{page:this.data.page}).then(res=>{
      let arr=res.data.data
      arr.forEach((ele, index) => {
        ele.selected=false
        ele.isTouchMove=false
      })
      this.setData({
        items: [...this.data.items,...arr]
      })
      if (!res.data.data.length) {
        this.setData({
          chudi: true,
          jiazai: false
        })
      } else {
        this.setData({
          chudi: false
        })
      }
    })
  },
  
  _getGuiGObj(e){
    this.setData({
      specInfo:e.detail
    })
    JsonPost('cat/update',e.detail).then(res=>{
      this.setData({
        items:[],
        page:1
      })
      this.getList()
    })

  },
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function(v, i) {

      if (v.isTouchMove) //只操作为true的

        v.isTouchMove = false;

    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items

    })
  },
  touchmove: function(e) {

    var that = this,

      index = e.currentTarget.dataset.index, //当前索引

      startX = that.data.startX, //开始X坐标

      startY = that.data.startY, //开始Y坐标

      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标

      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标

      //获取滑动角度

      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });

    that.data.items.forEach(function(v, i) {

      v.isTouchMove = false

      //滑动超过30度角 return

      if (Math.abs(angle) > 30) return;

      if (i == index) {

        if (touchMoveX > startX) //右滑

          v.isTouchMove = false

        else //左滑

          v.isTouchMove = true

      }

    })
    //更新数据

    that.setData({

      items: that.data.items

    })

  },
  /**

* 计算滑动角度

* @param {Object} start 起点坐标

* @param {Object} end 终点坐标

*/

  angle: function(start, end) {

    var _X = end.X - start.X,

      _Y = end.Y - start.Y

    //返回角度 /Math.atan()返回数字的反正切值

    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);

  },
  //删除事件

  del: function(e) {
    JsonPost('cat/del',{'cat_id':e.currentTarget.dataset.id}).then(res=>{
      this.setData({
        items:[],
        page:1
      })
      this.getList()
    })
  },
  //结算
  subMitPay(){
    if (this.data.checkNum==0)return
    var arr=[]
    this.data.items.forEach((ele)=>{
      if (ele.selected){
        arr.push(ele)
      }
    })    
    getApp().globalData.shopCartInfo=arr
    
    console.log(arr);
    wx.navigateTo({
      url: '/pages/orderPay/orderPay?ifCart=true'
    })
  },
  //前往扫码页
  goSaoMa() {
    wx.scanCode({　
      success: (res) => {　　
       console.log(res);
       let id=decodeURIComponent(res.result).split("?goods_id=")[1]
       wx.navigateTo({
         url: '/pages/goodsDetail/goodsDetail?goods_id='+id,
       })
      }　
  })
  },

  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.chudi) {
          this.setData({
            page: this.data.page + 1,
            jiazai: true
          })
          this.getList()
        }
      },

})